package com.ai.notices.service.impl;

import com.ai.notices.common.util.BeanUtil;
import com.ai.notices.common.util.PagedResult;
import com.ai.notices.common.util.StringUtil;
import com.ai.notices.dao.mysql.mapper.AuthStaffMapper;
import com.ai.notices.dao.mysql.model.AuthStaff;
import com.ai.notices.dao.mysql.model.AuthStaffExample;
import com.ai.notices.service.interfaces.IAuthStaffSV;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by huangzh on 17/4/13.
 */
@Service
public class AuthStaffSVImpl implements IAuthStaffSV {

    @Autowired
    private AuthStaffMapper authStaffMapper;

    public List<AuthStaff> getAuthStaff(AuthStaff authStaff)throws Exception{

        AuthStaffExample authStaffExample = new AuthStaffExample();
        AuthStaffExample.Criteria c = authStaffExample.createCriteria();

        if(StringUtil.isNotBlank(authStaff.getStaffCode())){
            c.andStaffCodeEqualTo(authStaff.getStaffCode());
        }
        if(StringUtil.isNotBlank(authStaff.getStaffFlag())){
            c.andStaffFlagEqualTo(authStaff.getStaffFlag());
        }
        List<AuthStaff> retList = authStaffMapper.selectByExample(authStaffExample);

        return retList;
    }

	@Override
	public PagedResult<AuthStaff> queryAuthStaffsByPage(AuthStaff staff, Integer pageNo, Integer pageSize)
			throws Exception {
		// TODO Auto-generated method stub
		pageNo = pageNo == null ? 1 : pageNo;
		pageSize = pageSize == null ? 10 : pageSize;
		PageHelper.startPage(pageNo, pageSize); // startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		AuthStaffExample example = new AuthStaffExample();
		AuthStaffExample.Criteria cri = example.createCriteria();
		if (!StringUtils.isEmpty(staff.getStaffName())) {
			cri.andStaffNameLike("%" + staff.getStaffName() + "%");
		}
		return BeanUtil.toPagedResult(authStaffMapper.selectByExample(example));
	}

	@Override
	public AuthStaff queryStaffByPk(String id) throws Exception {
		// TODO Auto-generated method stub
		return authStaffMapper.selectByPrimaryKey(Integer.valueOf(id));
	}
}
